/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009  Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.edu.ufcg.ccc.javalog.view;

import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import br.edu.ufcg.ccc.javalog.controller.FranchiseController;
import br.edu.ufcg.ccc.javalog.exceptions.FranchiseControllerException;
import br.edu.ufcg.ccc.javalog.model.Data;
import br.edu.ufcg.ccc.javalog.model.Package;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * View for managing the packages.
 * 
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 03/12/09
 */
@SuppressWarnings("serial")
public class PackageManagerView extends AbstractView {
	private JButton buttonAddPackage;
	private JButton buttonBack;
	private AbstractAction actionListPackages;
	private AbstractAction actionAddPackage;
	private JButton buttonSearchPackage;
	private AbstractAction actionBack;
	private JLabel labelPackageManager;
	private JButton buttonListPackages;
	private AbstractAction actionSearchPackage;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
			PackageManagerView inst = new PackageManagerView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				inst.setTitle("Gerenciador de Pacotes");
			}
		});
	}
	
	public PackageManagerView() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(getLabelPackageManager(), GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				.addGap(18)
				.addComponent(getButtonAddPackage(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(getButtonListPackages(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(getButtonSearchPackages(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(getButtonBack(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(50, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(getLabelPackageManager(), GroupLayout.Alignment.LEADING, 0, 371, Short.MAX_VALUE)
				    .addComponent(getButtonAddPackage(), GroupLayout.Alignment.LEADING, 0, 371, Short.MAX_VALUE)
				    .addComponent(getButtonListPackages(), GroupLayout.Alignment.LEADING, 0, 371, Short.MAX_VALUE)
				    .addComponent(getButtonSearchPackages(), GroupLayout.Alignment.LEADING, 0, 371, Short.MAX_VALUE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(143)
				        .addComponent(getButtonBack(), 0, 88, Short.MAX_VALUE)
				        .addGap(140)))
				.addContainerGap());
			thisLayout.linkSize(SwingConstants.VERTICAL, new Component[] {getButtonBack(), getButtonListPackages(), getButtonAddPackage()});
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Gerenciador de Pacotes");
			pack();
			this.setSize(403, 246);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	* This method should return an instance of this class which does 
	* NOT initialize it's GUI elements. This method is ONLY required by
	* Jigloo if the superclass of this class is abstract or non-public. It 
	* is not needed in any other situation.
	 */
	public static Object getGUIBuilderInstance() {
		return new PackageManagerView(Boolean.FALSE);
	}
	
	/**
	 * This constructor is used by the getGUIBuilderInstance method to
	 * provide an instance of this class which has not had it's GUI elements
	 * initialized (ie, initGUI is not called in this constructor).
	 */
	public PackageManagerView(Boolean initGUI) {
		super();
	}
	
	private JButton getButtonAddPackage() {
		if(buttonAddPackage == null) {
			buttonAddPackage = new JButton();
			buttonAddPackage.setText("Novo Pacote");
			buttonAddPackage.setAction(getActionAddPackage());
		}
		return buttonAddPackage;
	}
	
	private JButton getButtonListPackages() {
		if(buttonListPackages == null) {
			buttonListPackages = new JButton();
			buttonListPackages.setText("Listar Pacotes");
			buttonListPackages.setAction(getActionListPackages());
		}
		return buttonListPackages;
	}
	
	private JLabel getLabelPackageManager() {
		if(labelPackageManager == null) {
			labelPackageManager = new JLabel();
			labelPackageManager.setText("Gerenciamento de Pacotes");
			labelPackageManager.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return labelPackageManager;
	}
	
	private JButton getButtonBack() {
		if(buttonBack == null) {
			buttonBack = new JButton();
			buttonBack.setText("Voltar");
			buttonBack.setAction(getActionBack());
			buttonBack.setMnemonic(java.awt.event.KeyEvent.VK_V);
		}
		return buttonBack;
	}
	
	private AbstractAction getActionBack() {
		if(actionBack == null) {
			actionBack = new AbstractAction("Voltar", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return actionBack;
	}
	
	private AbstractAction getActionAddPackage() {
		if(actionAddPackage == null) {
			actionAddPackage = new AbstractAction("Novo Pacote", null) {
				public void actionPerformed(ActionEvent evt) {
					WindowManager.getInstance().openNewWindow(new AddPackageView());
				}
			};
			actionAddPackage.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_N));
		}
		return actionAddPackage;
	}
	
	private AbstractAction getActionListPackages() {
		if(actionListPackages == null) {
			actionListPackages = new AbstractAction("Listar Pacotes", null) {
				public void actionPerformed(ActionEvent evt) {
					WindowManager.getInstance().openNewWindow(new PackageListView(Data.getInstance().getHeadquarters()));
				}
			};
			actionListPackages.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_L));
		}
		return actionListPackages;
	}
	
	private JButton getButtonSearchPackages() {
		if(buttonSearchPackage == null) {
			buttonSearchPackage = new JButton();
			buttonSearchPackage.setText("Procurar Pacote");
			buttonSearchPackage.setAction(getActionSearchPackage());
		}
		return buttonSearchPackage;
	}
	
	private AbstractAction getActionSearchPackage() {
		if(actionSearchPackage == null) {
			actionSearchPackage = new AbstractAction("Procurar Pacote", null) {
				public void actionPerformed(ActionEvent evt) {
					String packageID = (String)JOptionPane.showInputDialog(
		                    PackageManagerView.this, "Identificador do pacote:", "Procura por ID",
		                    JOptionPane.QUESTION_MESSAGE);
					
					FranchiseController franchiseController = new FranchiseController(Data.getInstance().getHeadquarters());
					
					try {
						Package foundPackage = franchiseController.getPackage(packageID);
						if(foundPackage == null) {
							WindowManager.getInstance().openNewWindow(new PackageView(foundPackage));
						}
						else {
							throw new FranchiseControllerException("Falha ao tentar recuperar o pacote.");
						}
					} catch (FranchiseControllerException e) {
						JOptionPane.showMessageDialog(PackageManagerView.this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
					}
										
				}
			};
			actionSearchPackage.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_P));
		}
		return actionSearchPackage;
	}
}
